import axios from "axios";
import { ElMessage } from 'element-plus';

axios.defaults.baseURL = "http://127.0.0.1:10031";

export function message(msg, type) {
    ElMessage({
        message: msg,
        showClose: true,
        type: type,
        center: true
    })
}

export function messageNotClose(msg, type) {
    ElMessage({
        message: msg,
        showClose: true,
        type: type,
        center: true,
        duration: 0
    })
}

export const getRequest = (url, params) => {
    return axios({
        method: 'get',
        url: url,
        params: params,
        headers: {
            token: localStorage.getItem("token"),
            username: localStorage.getItem("username"),
            password: localStorage.getItem("password"),
        }
    })
}

export const postRequest = (url, data) => {
    return axios({
        method: 'post',
        url: url,
        data: data,
        headers: {
            token: localStorage.getItem("token"),
            username: localStorage.getItem("username"),
            password: localStorage.getItem("password"),
        }
    })
}

export const putRequest = (url, data) => {
    return axios({
        method: 'put',
        url: url,
        data: data,
        headers: {
            token: localStorage.getItem("token"),
            username: localStorage.getItem("username"),
            password: localStorage.getItem("password"),
        }
    })
}

export const deleteRequest = (url, data) => {
    return axios({
        method: 'delete',
        url: url,
        data: data,
        headers: {
            token: localStorage.getItem("token"),
            username: localStorage.getItem("username"),
            password: localStorage.getItem("password"),
        }
    })
}

export const downloadFileToLocal = (content, filename) => {
    var eleLink = document.createElement('a');
    eleLink.download = filename;
    eleLink.style.display = 'none';
    var blob = new Blob([content]);
    eleLink.href = URL.createObjectURL(blob);
    document.body.appendChild(eleLink);
    eleLink.click();
    document.body.removeChild(eleLink);
}

export const formatTime = (time) => {
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let today = time.getDate();
    return year + '/' + fillZero(month) + '/' + fillZero(today);
}

function fillZero(str) {
    var realNum;
    if (str < 10) {
        realNum = '0' + str;
    } else {
        realNum = str;
    }
    return realNum;
}